//
//  QWJSONObjectDesc.m
//  WJJSON
//
//  Created by Yunhai.Wu on 15/9/9.
//  Copyright (c) 2015年 WJ. All rights reserved.
//

#import "QWJSONObjectDesc.h"
#import "IQWJSONObject.h"

@implementation QWJSONPropertyDesc

-(void)setTypeName:(NSString *)typeName {
    _typeName = [typeName copy];
    if ([typeName isEqualToString:@"NSDate"]) {
        _type = QWObjectTypeDate;
    } else if ([typeName isEqualToString:@"NSString"]) {
        _type = QWObjectTypeString;
    } else if ([typeName isEqualToString:@"NSNumber"]) {
        _type = QWObjectTypeNumber;
    } else if ([typeName isEqualToString:@"NSSet"]) {
        _type = QWObjectTypeSet;
    } else if ([typeName isEqualToString:@"NSArray"]) {
        _type = QWObjectTypeArray;
    } else if ([typeName isEqualToString:@"NSDictionary"]) {
        _type = QWObjectTypeDictionary;
    } else {
        if ([NSClassFromString(typeName) conformsToProtocol:@protocol(IQWJSONObject)]) {
            _type = QWObjectTypeCustom;
        } else {
            _type = QWObjectTypeObject;
        }
    }
}

-(instancetype) initWithName:(NSString*) name typeName:(NSString*) typeName jsonName:(NSString*) jsonName nonJson:(BOOL) nonJson customHandle:(BOOL) cusHandle {
    self = [super init];
    if (self) {
        self.name = name;
        self.typeName = typeName;
        self.jsonName = jsonName;
        self.nonJson = nonJson;
        self.hasCusHandle = cusHandle;
    }
    return self;
}

-(BOOL)isEqual:(id)object {
    if ([object isKindOfClass:[QWJSONPropertyDesc class]]) {
        if ([_name isEqualToString:[(QWJSONPropertyDesc*)object name]]) {
            return YES;
        }
    }
    return NO;
}

-(NSUInteger)hash {
    return _name.hash;
}

@end



@interface QWJSONObjectDesc ()

@property (nonatomic, copy) NSArray *propertys;

@property (nonatomic, strong) NSMutableDictionary *jnToPropertyDict;

@property (nonatomic, strong) NSMutableDictionary *nToPropertyDict;

@end
@implementation QWJSONObjectDesc

-(NSArray*) allPropertys {
    return _propertys;
}

-(QWJSONPropertyDesc*) getPropertyByName:(NSString*) propertyName {
    return _nToPropertyDict[propertyName];
}

-(QWJSONPropertyDesc*) getPropertyByJsonName:(NSString*) jsonName {
    return _jnToPropertyDict[jsonName];
}

-(instancetype) initWithProperty:(NSArray*) propertys {
    self = [super init];
    if (self) {
        self.propertys = propertys;
        if ([self.propertys count] > 0) {
            self.jnToPropertyDict = [[NSMutableDictionary alloc] init];
            self.nToPropertyDict = [[NSMutableDictionary alloc] init];
        }
        for (QWJSONPropertyDesc *desc in self.propertys) {
            [self.jnToPropertyDict setObject:desc forKey:[desc jsonName]];
            [self.nToPropertyDict setObject:desc forKey:[desc name]];
        }
    }
    return self;
}
@end
